<?php

class User extends AppModel
{

    var $name = 'User';
    var $belongsTo = 'Group';
    var $actsAs = array('Acl' => 'requester');
    var $hasMany = array('Article', 'Evaluation');
    var $hasAndBelongsToMany = array(
        'Congress' => array(
            'className' => 'Congress',
            'joinTable' => 'congresses_users',
            'foreinKey' => 'user_id',
            'associationForeignKey' => 'congress_id'
        ),

        'Area' => array(
            'className' => 'Area',
            'joinTable' => 'areas_users',
            'foreinKey' => 'user_id',
            'associationForeignKey' => 'area_id'
        )
    );
    var $validate = array(
        'name' => array(
            'rule' => 'notEmpty',
            'message' => 'Não pode estar vazio'
        ),
        'cpf' => array(
            //TODO validar CPF
            'numeric' => array(
                'rule' => 'numeric',
                'message' => 'Deve conter apenas números'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Não pode estar vazio'
            )
        ),
        'email' => array(
            'email' => array(
                'rule' => 'email',
                'message' => 'E-mail inválido'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Não pode estar vazio'
            )
        ),
        'username' => array(
            'minLength' => array(
                'rule' => array('minLength', '4'),
                'message' => 'Mínimo de 4 caracteres'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Não pode estar vazio'
            ),
        ),
        'password' => array(
            'minLength' => array(
                'rule' => array('minLength', '6'),
                'message' => 'Mínimo de 6 caracteres'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Não pode estar vazio'
            ),
        )
    );

    function parentNode()
    {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        $data = $this->data;
        if (empty($this->data)) {
            $data = $this->read();
        }
        if (!$data['User']['group_id']) {
            return null;
        } else {
            return array('Group' => array('id' => $data['User']['group_id']));
        }
    }

    /**
     * Callback afterSave
     *
     * Atualiza o ARO para o usuário.
     *
     * @access public
     * @return void
     */
    function afterSave($created)
    {
        if (!$created) {
            $parent = $this->parentNode();
            $parent = $this->node($parent);
            $node = $this->node();
            $aro = $node[0];
            $aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
            $this->Aro->save($aro);
        }
    }

//    function bindNode($user)
//    {
//        return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
//    }
}

?>
